package cn.exempale.mscapplication.model.repository;


import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import cn.exempale.mscapplication.emnus.ResultType;
import cn.exempale.mscapplication.model.net.callback.BaseCallBack;
import cn.exempale.mscapplication.model.net.callback.MCallBack;
import cn.exempale.mscapplication.constant.UrlConstant;
import cn.exempale.mscapplication.model.net.service.ApiService;
import cn.exempale.mscapplication.model.net.callback.Resource;
import cn.exempale.mscapplication.utils.RetrofitUtils;
import cn.exempale.mscapplication.vo.BasicResultVO;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 文件上传接口调用
 */
public class FileRepository extends BaseRepository {

    private ApiService apiService;
    private static FileRepository instance;

    public static FileRepository newInstance() {
        if (instance == null)
            instance = new FileRepository();
        return instance;
    }

    private FileRepository() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlConstant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(RetrofitUtils.fileClient())
                .build();
        apiService = retrofit.create(ApiService.class);
    }
//
//    public LiveData<Resource> upLoad(Map<String, RequestBody> uploadDTO, MultipartBody.Part file) {
//        MutableLiveData<Resource> data = new MutableLiveData<>();
//        apiService.upload(uploadDTO, file).enqueue(new BaseCallBack<>(new MCallBack() {
//            @Override
//            public void success(LiveData<Resource<Object>> resourceData) {
//                super.success(resourceData);
//                data.setValue(Resource.success(resourceData.getValue().data));
//            }
//
//            @Override
//            public void error(LiveData<Resource<Object>> resourceData) {
//                super.error(resourceData);
//                data.setValue(Resource.error(resourceData.getValue().msg));
//            }
//        }));
//        return data;
//    }

    public Resource upLoad(Map<String, RequestBody> uploadDTO, MultipartBody.Part file) {
        try {
            Response<BasicResultVO> response = apiService.upload(uploadDTO, file).execute();
            if (response.isSuccessful()) {
                BasicResultVO vo = response.body();
                if (ResultType.match(vo.getOk()) == ResultType.SUCCESS) {
                    return Resource.success(vo.getData());
                } else {
                    return Resource.error(vo.getFailed());
                }
            } else {
                return Resource.error("请示失败");
            }
        } catch (IOException ioException) {
            ioException.printStackTrace();
            return Resource.error("网络异常,请求失败");
        }
    }
}
